package com.xtxk.service.user;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xtxk.entity.OrderEntity;
import com.xtxk.entity.ProductEntity;
import com.xtxk.mapper.OrderMapper;
import com.xtxk.mapper.ProductMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper,OrderEntity> implements IOrderService {

    @Resource
    private ProductMapper productMapper;



    @Transactional// 加上事物控制
    @Override
    public void secKill(ProductEntity pd) {
        // 首先减少商品库存
        int i = productMapper.updateStoreNum(pd.getGoodsId(), pd.getGoodsNumber());
        if (i == 0) {
            throw new NullPointerException();// 抛出异常
        }

        // 创建订单
        OrderEntity entity = new OrderEntity();
        entity.setGoodsId(pd.getGoodsId());
        entity.setOrderPrice(pd.getGoodsPrice());
        save(entity);
    }
}
